package com.lanyou.esb.cook.proxy.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.lanyou.esb.cook.proxy.common.DateUtils;
import com.lanyou.esb.cook.proxy.entity.IfAlarm;

/**
 * 服务告警持久层
 * 
 * @author Davey.wu
 */
public class IfAlarmDao extends BaseDao<IfAlarm> {

	private static final List<String> FIELDS = new ArrayList<String>();

	/** SQL **/
	private static final String SQL;

	static {
		FIELDS.add("IF_ALARM_ID");
		FIELDS.add("IF_ID");
		FIELDS.add("IF_TYPE");
		FIELDS.add("IF_NAME");
		FIELDS.add("FIELD_SYSTEM_ID");
		FIELDS.add("SYSTEM_CODE");
		FIELDS.add("ALARM_TYPE");
		FIELDS.add("ALARM_LEVEL");
		FIELDS.add("IN_ESB_DATE");
		FIELDS.add("OUT_ESB_DATE");
		FIELDS.add("PLATFORM_REQUEST_DATE");
		FIELDS.add("PLATFORM_RESPONSE_DATE");
		FIELDS.add("FLOW");
		FIELDS.add("ANSWER_TIMES");
		FIELDS.add("FLOW_THRESHOLD");
		FIELDS.add("ERROR_DATE");
		FIELDS.add("ERROR_RECEIVER");
		FIELDS.add("ASSIGN_WORK_CODE");
		FIELDS.add("IF_ALARM_REPEAT");
		FIELDS.add("ALARM_PEAK_VALUE");

		FIELDS.add("REQUEST_IP");
		FIELDS.add("REQUEST_METHOD");
		FIELDS.add("REQUEST_URL");
		FIELDS.add("REQUEST_MESSAGE");
		FIELDS.add("RESPONSE_CODE");

		FIELDS.add("IS_ENABLE");
		FIELDS.add("CREATOR");
		FIELDS.add("CREATED_DATE");
		FIELDS.add("MODIFIER");
		FIELDS.add("LAST_UPDATED_DATE");
	}
	static {
		StringBuilder builderFields = new StringBuilder();
		StringBuilder builderValues = new StringBuilder();
		builderFields.append(" (");
		builderValues.append(" (");
		int size = FIELDS.size();
		for (int i = 0; i < size; ++i) {
			builderFields.append(FIELDS.get(i) + (i == size - 1 ? "" : ","));
			builderValues.append((i == size - 1 ? "?" : "?,"));
		}
		builderFields.append(") ");
		builderValues.append(") ");
		SQL = "insert into t_esb_bu_if_alarm" + builderFields.toString()
				+ "values " + builderValues.toString();
	}

	@Override
	public int save(IfAlarm alarm) {

		return getJdbcTemplate()
				.update(SQL,
						alarm.getId(),
						alarm.getIfId(),
						alarm.getIfType(),
						alarm.getIfName(),
						alarm.getSystemId(),
						alarm.getSystemCode(),
						alarm.getAlarmType(),
						alarm.getAlarmLevel(),
						alarm.getInEsbDate() != null ? DateUtils.DATEPATTERN_YYYYMMDDHHMMssSSS
								.format(alarm.getInEsbDate()) : null,
						alarm.getOutEsbDate() != null ? DateUtils.DATEPATTERN_YYYYMMDDHHMMssSSS
								.format(alarm.getOutEsbDate()) : null,
						alarm.getPlatReqDate() != null ? DateUtils.DATEPATTERN_YYYYMMDDHHMMssSSS
								.format(alarm.getPlatReqDate()) : null,
						alarm.getPlatResDate() != null ? DateUtils.DATEPATTERN_YYYYMMDDHHMMssSSS
								.format(alarm.getPlatResDate()) : null,
						alarm.getFlow(), alarm.getAnswerTime(),
						alarm.getFowThreshold(), alarm.getErrorDate(),
						alarm.getErrorReceiver(), alarm.getAssignWorkCode(),
						alarm.getRepeat(), alarm.getPeakValue(),
						alarm.getReqIp(), alarm.getReqMethod(),
						alarm.getReqUrl(), alarm.getReqMess(),
						alarm.getResCode(), alarm.getIsEnable(),
						alarm.getCreator(), alarm.getCreateDate(),
						alarm.getModifier(), alarm.getLastUpdatedDate());
	}

	@Override
	public int saveAll(List<IfAlarm> alarms) {
		if (alarms == null || alarms.isEmpty()) {
			return 0;
		}
		List<Object[]> args = new ArrayList<>(alarms.size());
		int i = 0;
		for (IfAlarm alarm : alarms) {
			i = 0;
			Object[] objects = new Object[FIELDS.size()];
			objects[i++] = alarm.getId();
			objects[i++] = alarm.getIfId();
			objects[i++] = alarm.getIfType();
			objects[i++] = alarm.getIfName();
			objects[i++] = alarm.getSystemId();
			objects[i++] = alarm.getSystemCode();
			objects[i++] = alarm.getAlarmType();
			objects[i++] = alarm.getAlarmLevel();
			objects[i++] = alarm.getInEsbDate() != null ? DateUtils.DATEPATTERN_YYYYMMDDHHMMssSSS
					.format(alarm.getInEsbDate()) : null;
			objects[i++] = alarm.getOutEsbDate() != null ? DateUtils.DATEPATTERN_YYYYMMDDHHMMssSSS
					.format(alarm.getOutEsbDate()) : null;
			objects[i++] = alarm.getPlatReqDate() != null ? DateUtils.DATEPATTERN_YYYYMMDDHHMMssSSS
					.format(alarm.getPlatReqDate()) : null;
			objects[i++] = alarm.getPlatResDate() != null ? DateUtils.DATEPATTERN_YYYYMMDDHHMMssSSS
					.format(alarm.getPlatResDate()) : null;
			objects[i++] = alarm.getFlow();
			objects[i++] = alarm.getAnswerTime();
			objects[i++] = alarm.getFowThreshold();
			objects[i++] = alarm.getErrorDate();
			objects[i++] = alarm.getErrorReceiver();
			objects[i++] = alarm.getAssignWorkCode();
			objects[i++] = alarm.getRepeat();
			objects[i++] = alarm.getPeakValue();
			objects[i++] = alarm.getReqIp();
			objects[i++] = alarm.getReqMethod();
			objects[i++] = alarm.getReqUrl();
			objects[i++] = alarm.getReqMess();
			objects[i++] = alarm.getResCode();
			objects[i++] = alarm.getIsEnable();
			objects[i++] = alarm.getCreator();
			objects[i++] = alarm.getCreateDate();
			objects[i++] = alarm.getModifier();
			objects[i++] = alarm.getLastUpdatedDate();
			args.add(objects);
		}
		int[] results = getJdbcTemplate().batchUpdate(SQL, args);
		if (results == null) {
			return 0;
		}
		int result = 0;
		for (int k : results) {
			result += k;
		}
		return result;
	}

	@Override
	public int update(List<IfAlarm> alarms) {
		if (alarms == null || alarms.isEmpty()) {
			return 0;
		}
		List<Object[]> args = new ArrayList<>(alarms.size());
		int i = 0;
		for (IfAlarm alarm : alarms) {
			i = 0;
			Object[] objects = new Object[2];
			objects[i++] = alarm.getRepeat();
			objects[i++] = alarm.getId();
			args.add(objects);
		}
		String sql = "UPDATE t_esb_bu_if_alarm SET IF_ALARM_REPEAT=? WHERE IF_ALARM_ID=?";
		int[] results = getJdbcTemplate().batchUpdate(sql, args);
		if (results == null) {
			return 0;
		}
		int result = 0;
		for (int k : results) {
			result += k;
		}
		return result;
	}
}
